#include "keyscan.h"
#include "delay.h"
#include <REGX51.H>
/*F**************************************************************************
* NAME:Keyscan 
*----------------------------------------------------------------------------
* AUTHOR:         $卢建成$
* MODIFIED DATE : $2016/4/8$
*----------------------------------------------------------------------------
* PURPOSE:扫描4*4键盘
          按键与对应的输出码：  
		          0  1  2  3
		          4  5  6  7
				  8  9  A  B
				  C  D  E  F

*   
*----------------------------------------------------------------------------
* PARAMS: no
*   
*
* return: 键值
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:	  键值格式：有键按下时，低4位是相应的键的编码，其余位为0
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
unsigned char Keyscan(void)
{
	unsigned char i;
	unsigned char temp=0x01;
	unsigned char DATA;
	ScanPort=0x0F; 	 //低位为1，保证串口
	while(ScanPort==0x0F);//等待有键按下
	Delay10ms();		  //延时去抖
	for(i=0;i<4;i++)
	{	  
	  ScanPort=0xFF-temp;//逐行扫描
	  temp<<=1;
	  if((ScanPort&0xF0)!=0xF0)// 取高位
	     {	
	           DATA=0xFF-ScanPort;//读扫描口，并取反			   
			   DATA>>=4;
			   switch (DATA)
			   {
				case 0x01:DATA=0;break;
				case 0x02:DATA=1;break;
				case 0x04:DATA=2;break;
				case 0x08:DATA=3;break;
				default:DATA=0; 
			   }
			   DATA=i*4+DATA;

	      }

	 }
	ScanPort=0x0F;
	while(ScanPort!=0x0F);//等待按键松开
    return DATA;
}